package com.home.homepage.service.headNav.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.homepageentity.headNav.TbHeadNav;
import com.eig.common.vo.Result;
import com.home.homepage.dao.headNav.HeadDao;
import com.home.homepage.service.headNav.HeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author ：YQ袁（2446806940@qq.com）
 * @date ：Created in 2022/12/8 19:58
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HeadServiceImpl implements HeadService {
    @Autowired
    private HeadDao headDao;
    @Override
    public Result getAllHeadNav(Integer current, Integer pageSize, TbHeadNav tbHeadNav) {
        IPage<TbHeadNav> page = new Page<>(current,pageSize);
        QueryWrapper<TbHeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StringUtils.hasText(tbHeadNav.getHeadName())){
            wrapper.like("head_name" , tbHeadNav.getHeadName());
        }
        if (StringUtils.hasText(tbHeadNav.getIsDiable())){
            wrapper.eq("is_diable" , tbHeadNav.getIsDiable());
        }
        IPage<TbHeadNav> page1 = headDao.selectPage(page, wrapper);
        if (page1!=null){
            return new Result(2000,"查询成功",page1);
        }
        return new Result(2000,"查询失败");
    }

    @Override
    public Result deleteHeadNav(TbHeadNav tbHeadNav) {
        int i = headDao.deleteById(tbHeadNav.getHeadId());
        if (i>0){
            return new Result(2000,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result changeStatus(String headId, String isDiable) {
        UpdateWrapper<TbHeadNav> wrapper = new UpdateWrapper<>();
        wrapper.eq("head_id",headId);
        wrapper.set("is_diable",isDiable);
        int update = headDao.update(null, wrapper);
        if (update>0){
            return new Result(2000,"状态修改成功");
        }
        return new Result(5000 ,"状态修改失败");
    }

    @Override
    public Result addHeadNav(TbHeadNav tbHeadNav) {
        TbHeadNav aa = new TbHeadNav();
        aa.setHeadId(tbHeadNav.getHeadId());
        aa.setHeadName(tbHeadNav.getHeadName());
        aa.setHref(tbHeadNav.getHref());
        aa.setIsDiable("0");
        aa.setIsDelete("0");
        int insert = headDao.insert(aa);
        if (insert>0 ){
            return new Result(2000,"添加成功");

        }
        return new Result(5000,"添加失败");
    }
}
